﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Rison.BusinessFacade.Helper
{
    public sealed class StringHelper
    {
        public static bool IsNumber(string value)
        {
            if (IsNullOrEmpty(value))
                return false;

            try
            {
                long n = Convert.ToInt64(value);

                return true;
            }
            catch { }

            return false;
        }

        /// <summary>
        /// 判断字符串是否为空。
        /// 对全是空格的字符串，也将认为是空的。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(string value)
        {
            if (value != null)
                if (value.Trim().Length > 0)
                    return false;

            return true;
        }

        public static bool IsDate(string value)
        {
            try
            {
                DateTime t = DateTime.Parse(value);
                return true;
            }
            catch { }
            return false;
        }
        

        public static string GetValue(object value)
        {
            if (value != null)
                return value.ToString();
            return string.Empty;
        }

        public static string GetValue(DataRow row, string fieldName)
        {
            if (row.Table.Columns.Contains(fieldName))
                if (!System.DBNull.Value.Equals(row[fieldName]))
                    return row[fieldName].ToString();
            return string.Empty;
        }

        public static string GetValue(DataRow row, int fieldIndex)
        {
            if (!System.DBNull.Value.Equals(row[fieldIndex]))
                return row[fieldIndex].ToString();
            return string.Empty;
        }

        /// <summary>
        /// 返回日期。
        /// </summary>
        /// <param name="row"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeValue(DataRow row, string fieldName)
        {
            if (!System.DBNull.Value.Equals(row[fieldName]))
                return Convert.ToDateTime(row[fieldName].ToString());

            return DateTime.Now;
        }

        /// <summary>
        /// 验证字符串长度。
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns>在length范围内，返回true。</returns>
        public static bool ValidLength(string value, int length)
        {
            return (System.Text.Encoding.Default.GetByteCount(value) <= length);
        }


        /// <summary>
        /// 判断字符串是否由、且仅仅由字符或者数字组成。
        /// </summary>
        /// <param name="value">需要进行判断的字符串对象。</param>
        /// <returns>如果字符串符合“由、且仅仅由字符或者数字组成”，那么返回True，否则返回False</returns>
        /// <remarks>
        /// </remarks>
        public static bool IsMergeByNumberAndChar(string value)
        {
            return IsMergeByNumberAndChar(value, string.Empty);
        }


        /// 判断字符串是否由、且仅仅由字符或者数字组成。
        /// </summary>
        /// <param name="value">需要进行判断的字符串对象。</param>
        /// <param name="otherChars">除数字、字符外，允许的其他字符。如下划线</param>
        /// <returns>如果字符串符合“由、且仅仅由字符或者数字组成”，那么返回True，否则返回False</returns>
        /// <remarks>
        /// </remarks>
        public static bool IsMergeByNumberAndChar(string value, string otherChars)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(value);

            for (int index = 0; index <= bytes.Length - 1; index++)
            {
                byte b = bytes[index];

                if ((b >= 48 && b <= 57) || (b >= 97 && b <= 122) || (b >= 65 && b <= 90))
                {
                }
                else if (!string.IsNullOrEmpty(otherChars) && otherChars.IndexOf(Convert.ToChar(b)) >= 0)
                {
                }
                else
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 整除
        /// </summary>
        /// <param name="divisor">除数</param>
        /// <param name="dividend">被除数</param>
        /// <returns></returns>
        public static int ExactDivide(int divisor, int dividend)
        {
            return Convert.ToInt32((divisor - (divisor % dividend)) / dividend);
        }


        /// <summary>
        /// 读取指定长度的随机串
        /// </summary>
        /// <param name="length">长度</param>
        /// <param name="numberOnly">是否只包含数字</param>
        /// <returns></returns>
        public static string GetRadomString(int length, bool numberOnly)
        {
            lock (typeof(StringHelper))
            {
                Random r = new Random();

                StringBuilder str = new StringBuilder();

                if (numberOnly)
                {
                    for (int i = 1; i <= length; i++)
                    {
                        str.Append(r.Next(10).ToString());
                    }
                }
                else
                {
                    while (str.Length < length)
                    {
                        int val = r.Next(48, 122);//0-9 a-z A-Z

                        if ((val >= 48 && val <= 57) || (val >= 65 && val <= 90) || (val >= 97 && val <= 122))
                        {
                            str.Append(Convert.ToChar(val).ToString());
                        }
                    }
                }

                r = null;

                return str.ToString();
            }
        }

    }
}
